package dao.Impl;

import dao.CourseStudentDao;
import po.CourseStudent;
import util.DBUtils;
import util.DaoRepeat;

import java.sql.SQLException;
import java.util.List;

public class CourseStudentDaoImpl  extends BaseDaoImpl<CourseStudent> implements CourseStudentDao {


    @Override
    public List<CourseStudent> queryAll(Integer sid) {
        List<CourseStudent> courseStudents = queryMany("select * from CourseStudent where sid=?", CourseStudent.class, sid);
        return  courseStudents;
    }

    @Override
    public boolean insert(CourseStudent courseStudent) {
        try {
            insert(courseStudent, DBUtils.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  true;
    }

    @Override
    public boolean delete(CourseStudent courseStudent) {
        try {
            delete(courseStudent,DBUtils.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  true;
    }
    @Override
    public Integer  haveChooseNumber(Integer id) {
        DaoRepeat<CourseStudent> daoRepeat = new DaoRepeat<>();
        int number = daoRepeat.getNumber("select * from CourseStudent where cid=?", id);
        return  number;
    }

    @Override
    public boolean ifHaveChoose(Integer cid, Integer sid) {
        CourseStudent course_student = queryOne("select * from CourseStudent where cid=? and sid=?", CourseStudent.class, cid, sid);
        if(course_student!=null){
            return  true;
        }else{
            return false;
        }
    }

    @Override
    public CourseStudent queryOneCourseStudent(Integer cid, Integer sid) {
        CourseStudent courseStudent = queryOne("select * from CourseStudent where cid=? and sid=?", CourseStudent.class, cid, sid);
        return  courseStudent;
    }

}
